package com.atguigu.dw.gmallpublisher.controll;

import com.alibaba.fastjson.JSON;
import com.atguigu.dw.gmallpublisher.service.DauServeice;
import com.atguigu.dw.gmallpublisher.service.OrderServeice;
import com.atguigu.dw.gmallpublisher.service.SaleDetailService;
import com.atguigu.util.MyDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：剧情再美终是戏
 * @description：查询日活数据controll
 * @mail : 13286520398@163.com
 * @date ：Created in 2020/2/12 12:40
 * @modified By：
 * @version: 1.0
 */
@RestController
public class PublisherController {

    @Autowired
    DauServeice dauServeice;

    @Autowired
    OrderServeice orderServeice;

    /*
    [
      {"id":"dau","name":"新增日活","value":1200},
      {"id":"new_mid","name":"新增用户","value":233}
      {"id":"order_amount","name":"新增交易额","value":1000.2 }
    ]
     */
    @GetMapping("/realtime-total")
    public String getTotal(@RequestParam("date") String date) {

        // 返回结果
        List<Map> result = new ArrayList<>();

        // 新增日活
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", "dau");
        map1.put("name", "新增日活");
        map1.put("value", dauServeice.getDauTotal(date));
        result.add(map1);

        // 新增用户
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", "new_mids");
        map2.put("name", "新增用户");
        map2.put("value", 999);
        result.add(map2);

        // 订单日总数
        Map<String, Object> map3 = new HashMap<>();
        map3.put("id", "order_amount");
        map3.put("name", "新增交易额");
        map3.put("value", orderServeice.getOrderTotal(date));
        result.add(map3);

        // 将结果返回
        return JSON.toJSONString(result);
    }

    /*
    {
       "yesterday":{"11":383,"12":123,"17":88,"19":200 },
       "today":{"12":38,"13":1233,"17":123,"19":688 }
    }
     */
    @GetMapping("/realtime-hour")
    public String getRealtimeHour(@RequestParam("id") String id, @RequestParam("date") String date) {
        // 定义返回结果
        Map<String, Map> resultMap = new HashMap<>();

        // 日活情况
        if ("dau".equals(id)) {
            // 获取当天数据
            Map<String, Long> today = dauServeice.getRealtimeHour(date);
            resultMap.put("today", today);

            // 获取前一天的数据
            Map<String, Long> yesterday = dauServeice
                    .getRealtimeHour(MyDateUtil.getStepsTime(LocalDate.parse(date), 1, "days").toString());
            resultMap.put("yesterday", yesterday);
        } else if ("order_amount".equals(id)) {
            // 获取当天数据
            Map<String, Double> today = orderServeice.getRealtimeHour(date);
            resultMap.put("today", today);

            // 获取前一天的数据
            Map<String, Double> yesterday = orderServeice
                    .getRealtimeHour(MyDateUtil.getStepsTime(LocalDate.parse(date), 1, "days").toString());
            resultMap.put("yesterday", yesterday);
        }
        // 将结果返回
        return JSON.toJSONString(resultMap);
    }

    @Autowired
    SaleDetailService saleDetailService;

    @GetMapping("/sale_detail")
    public String saleDetail(@RequestParam("date") String date,
                             @RequestParam("startpage") int startPage,
                             @RequestParam("size") int pageSize,
                             @RequestParam("aggField") String aggField,
                             @RequestParam("keyword") String keyword) {
        // user_gender user_age
        // http://localhost:9012/sale_detail?date=2019-05-20&&startpage=1&&size=5&&keyword=手机小米&aggField=user_gender
        // http://localhost:9012/sale_detail?date=2019-05-20&&startpage=1&&size=5&&keyword=手机小米&aggField=user_age
        return saleDetailService.getSaleDetailAndAggResultByAggField(date, keyword, startPage, pageSize, aggField, 2);
    }
}
